package com.example.myapplication6;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ChatMsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int CHAT_MSG_TYPE_SEND_TEXT = 0xf1;

    private static final int CHAT_MSG_TYPE_RECV_TEXT = 0xf2;

    private List<ChatMsgModel> dataList;

    private Context context;

    public ChatMsgAdapter(Context context) {
        this.context = context;
    }

    public void setDataList(List<ChatMsgModel> dataList) {
        this.dataList = dataList;
    }

    //判断不同条目数据，用于生成不同ViewHolder
    @Override
    public int getItemViewType(int position) {
        //true为发送/false为接受
        if (dataList.get(position).isSendOrRecv())
            return CHAT_MSG_TYPE_SEND_TEXT;
        else return CHAT_MSG_TYPE_RECV_TEXT;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case CHAT_MSG_TYPE_SEND_TEXT:
                return new ChatMsgSendHolder(LayoutInflater.from(context)
                        .inflate(R.layout.chat_msg_item_text_send, null));

            default:
                return new ChatMsgRecvHolder(LayoutInflater.from(context)
                        .inflate(R.layout.chat_msg_item_text_recv, null));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //方法1 调用getItemViewType()
        switch (getItemViewType(position)) {
            case CHAT_MSG_TYPE_SEND_TEXT:
                ((ChatMsgSendHolder) holder).sendTxtContent.setText(
                        dataList.get(position).getMsgTextContent());
                ((ChatMsgSendHolder) holder).sendTxtIcon.setImageResource(R.mipmap.ic_launcher);
                break;

            default:
                ((ChatMsgRecvHolder) holder).recvTxtCntent.setText(
                        dataList.get(position).getMsgTextContent());
                ((ChatMsgRecvHolder) holder).recvTxtImage.setImageResource(R.mipmap.ic_launcher);
                break;
        }

        //方法2 instanceof
        if (holder instanceof ChatMsgSendHolder) {

            ((ChatMsgSendHolder) holder).sendTxtContent.setText(
                    dataList.get(position).getMsgTextContent());
            ((ChatMsgSendHolder) holder).sendTxtIcon.setImageResource(R.mipmap.ic_launcher);
        } else if (holder instanceof ChatMsgRecvHolder) {

            ((ChatMsgRecvHolder) holder).recvTxtCntent.setText(
                    dataList.get(position).getMsgTextContent());
            ((ChatMsgRecvHolder) holder).recvTxtImage.setImageResource(R.mipmap.ic_launcher);
        }
    }

    @Override
    public int getItemCount() {
        return null != dataList ? dataList.size() : 0;
    }

    class ChatMsgSendHolder extends RecyclerView.ViewHolder {

        TextView sendTxtContent;

        ImageView sendTxtIcon;

        public ChatMsgSendHolder(@NonNull View itemView) {
            super(itemView);

            sendTxtContent = itemView.findViewById(R.id.text_chat_msg_item_send_content);
            sendTxtIcon = itemView.findViewById(R.id.image_chat_msg_item_send_icon);
        }
    }

    class ChatMsgRecvHolder extends RecyclerView.ViewHolder {

        TextView recvTxtCntent;

        ImageView recvTxtImage;

        public ChatMsgRecvHolder(@NonNull View itemView) {
            super(itemView);

            recvTxtCntent = itemView.findViewById(R.id.text_chat_msg_item_recv_content);
            recvTxtImage = itemView.findViewById(R.id.image_chat_msg_item_recv_icon);
        }
    }
}
